home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Enigma Amiga CD / Listati / 66-Lug-Ago-DIDATTICA-List1.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  10KB  |  289 lines

  1. /****************************************************************************
  2. * Listato 1                                                                 *
  3. * Vi ricordate l'esempio di qualche numero fa di una finestra SuperBitMap   *
  4. * con un immagine 1024x1024 che poteva scorrere nella finestra mediante     *
  5. * tasti cursore? L'esempio e' stato adattato con l'utilizzo di gadget pro-  *
  6. * porzionali per lo spostamento della bitmap                                *
  7. ****************************************************************************/
  8.  
  9. #define INTUI_V36_NAMES_ONLY
  10.  
  11. /* inclusione file di supporto */
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/screens.h>
  16. #include <graphics/gfx.h>
  17. #include <clib/exec_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/graphics_protos.h>
  20. #include <clib/layers_protos.h>
  21. #include <clib/dos_protos.h>
  22.  
  23. struct Library *IntuitionBase = NULL;     /* puntatore a intuition.library */
  24. struct Library *GfxBase = NULL;           /* puntatore a graphics.library */
  25. struct Library *LayersBase = NULL;        /* puntatore a layers.library */
  26. struct Screen *pubscreen = NULL;          /* puntatore al workbench */
  27. struct Window *finestra = NULL;           /* puntatore alla finestra */
  28. struct BitMap *supBitMap = NULL;          /* puntatore alla bitmap */
  29. struct MsgPort *UPort;                    /* puntatore alla porta IDCMP */
  30.  
  31. /* definizione codici identificatori dei gagdets */
  32. #define ORIZGADGET  0
  33. #define VERTGADGET  1
  34. #define NESSUNGADGET 0xFFFF
  35.  
  36. /* strutture per i gadgets */
  37. struct Image GadOrizImage, GadVertImage;
  38. struct PropInfo PropVertInfo, PropOrizInfo;
  39. struct Gadget GadVert, GadOriz;
  40.  
  41. /* definizione prototipi di funzione */
  42. void WaitEvent(struct MsgPort *,struct IntuiMessage *);
  43. void CloseAll(void);
  44. void OpenAll(void);
  45. void InizializzaGadgets(void);
  46. void DisegnaTutto(void);
  47. void NewSize(LONG *, LONG *);
  48. void MoveMap(UWORD, LONG *, LONG *);
  49. UWORD RangeRand(unsigned long MaxValue); /* funzione di Amiga.lib per la generazione di numeri casuali */
  50.  
  51. void WaitEvent(struct MsgPort *porta,struct IntuiMessage *mess)
  52. {
  53.   struct IntuiMessage *msg;
  54.  
  55.   while ((msg = (struct IntuiMessage *)GetMsg(porta)) == NULL)
  56.     WaitPort(porta);
  57.  
  58.   CopyMem(msg,mess,sizeof(struct IntuiMessage));
  59.   return;
  60. }
  61.  
  62. /* procedura CloseAll() */
  63. void CloseAll()
  64. {
  65.   register ULONG i;
  66.  
  67.   if (finestra != NULL) CloseWindow(finestra);
  68.   for (i=0; i<pubscreen->BitMap.Depth; i++)
  69.     if (supBitMap->Planes[i] != NULL) FreeRaster(supBitMap->Planes[i],1024,1024);
  70.   if (supBitMap != NULL) FreeMem(supBitMap,sizeof(struct BitMap));
  71.   if (pubscreen != NULL) UnlockPubScreen(NULL,pubscreen);
  72.   if (LayersBase != NULL) CloseLibrary(LayersBase);
  73.   if (GfxBase != NULL) CloseLibrary(GfxBase);
  74.   if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  75.  
  76.   exit(0);
  77. }
  78.  
  79. /* procedura OpenAll() */
  80. void OpenAll()
  81. {
  82.   register ULONG i;
  83.  
  84.   /* apriamo intuition.library, almeno version 36 */
  85.   if ((IntuitionBase = OpenLibrary("intuition.library",36L)) == NULL)
  86.     CloseAll();
  87.  
  88.   /* apriamo graphics.library, almeno version 36; vengono utilizzate delle
  89.      funzioni per il disegno */
  90.   if ((GfxBase = OpenLibrary("graphics.library",36L)) == NULL)
  91.     CloseAll();
  92.  
  93.   /* apriamo layers.library, almeno version 36; necessita per l'utilizzo di una
  94.      funzione per lo scrolling del layer della finestra */
  95.   if ((LayersBase = OpenLibrary("layers.library",36L)) == NULL)
  96.     CloseAll();
  97.  
  98.   /* blocca lo schermo pubblico di default */
  99.   if ((pubscreen = LockPubScreen(NULL)) == NULL)
  100.     CloseAll();
  101.  
  102.   /* alloca la memoria per la struttura BitMap */
  103.   if ((supBitMap = (struct BitMap *)AllocMem(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  104.     CloseAll();
  105.   /* inizializza i campi della BitMap */
  106.   InitBitMap(supBitMap,pubscreen->BitMap.Depth,1024,1024);
  107.   /* alloca i piani di bit della BitMap */
  108.   for (i=0; i<pubscreen->BitMap.Depth; i++)
  109.   {
  110.     if ((supBitMap->Planes[i] = AllocRaster(1024,1024)) == NULL)
  111.       CloseAll();
  112.   }
  113.   
  114.   /* Inizializza i campi dei gadgets */
  115.   InizializzaGadgets();
  116.   /* apre finestra spostabile, ridimensionabile, SuperBitMap, GimmeZeroZero,
  117.      con il gadget di chiusura */
  118.   if ((finestra = OpenWindowTags(NULL,WA_Left,10,
  119.                                       WA_Top,10,
  120.                                       WA_Width,350,
  121.                                       WA_Height,150,
  122.                                       WA_MaxWidth,1024,
  123.                                       WA_MaxHeight,1024,
  124.                                       WA_Title,"Mostra BitMap",
  125.                                       WA_DragBar,TRUE,
  126.                                       WA_CloseGadget,TRUE,
  127.                                       WA_DepthGadget,TRUE,
  128.                                       WA_SizeGadget,TRUE,
  129.                                       WA_SizeBRight,TRUE,
  130.                                       WA_SizeBBottom,TRUE,
  131.                                       WA_Flags,WFLG_SUPER_BITMAP|WFLG_GIMMEZEROZERO|WFLG_NOCAREREFRESH,
  132.                                       WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_GADGETDOWN|IDCMP_GADGETUP|IDCMP_NEWSIZE,
  133.                                       WA_Gadgets,&GadOriz,
  134.                                       WA_PubScreen,pubscreen,
  135.                                       WA_SuperBitMap,supBitMap,
  136.                                       TAG_END)) == NULL)
  137.     CloseAll();
  138.   UPort = finestra -> UserPort;
  139.  
  140.   return;
  141. }
  142.  
  143. /* Questa procedura inizializza i due gadgets proporzionali */
  144. void InizializzaGadgets()
  145. {
  146.   PropOrizInfo.Flags = AUTOKNOB|FREEHORIZ|PROPNEWLOOK;
  147.   PropOrizInfo.HorizPot = 0;
  148.   PropOrizInfo.VertPot = 0;
  149.   PropOrizInfo.HorizBody = MAXBODY;
  150.   PropOrizInfo.VertBody = MAXBODY;
  151.   GadOriz.NextGadget = &GadVert;
  152.   GadOriz.LeftEdge = 3; GadOriz.TopEdge = -7;
  153.   GadOriz.Width = -23; GadOriz.Height = 6;
  154.   GadOriz.Flags = GFLG_RELBOTTOM|GFLG_RELWIDTH;
  155.   GadOriz.Activation = GACT_RELVERIFY|GACT_IMMEDIATE|GACT_BOTTOMBORDER;
  156.   GadOriz.GadgetType = GTYP_PROPGADGET|GTYP_GZZGADGET;
  157.   GadOriz.GadgetRender = (APTR)&GadOrizImage;
  158.   GadOriz.SpecialInfo = (APTR)&PropOrizInfo;
  159.   GadOriz.GadgetID = ORIZGADGET;
  160.  
  161.   PropVertInfo.Flags = AUTOKNOB|FREEVERT|PROPNEWLOOK;
  162.   PropVertInfo.HorizPot = 0;
  163.   PropVertInfo.VertPot = 0;
  164.   PropVertInfo.HorizBody = MAXBODY;
  165.   PropVertInfo.VertBody = MAXBODY;
  166.   GadVert.NextGadget = NULL;
  167.   GadVert.LeftEdge = -14; GadVert.TopEdge = pubscreen->WBorTop+pubscreen->Font->ta_YSize + 2;
  168.   GadVert.Width = 12; GadVert.Height = -GadVert.TopEdge - 11;
  169.   GadVert.Flags = GFLG_RELRIGHT|GFLG_RELHEIGHT;
  170.   GadVert.Activation = GACT_RELVERIFY|GACT_IMMEDIATE|GACT_RIGHTBORDER;
  171.   GadVert.GadgetType = GTYP_PROPGADGET|GTYP_GZZGADGET;
  172.   GadVert.GadgetRender = (APTR)&GadVertImage;
  173.   GadVert.SpecialInfo = (APTR)&PropVertInfo;
  174.   GadVert.GadgetID = VERTGADGET;
  175.  
  176.   return;
  177. }
  178.  
  179. /* Questa procedura disegna una serie di linee nella BitMap della finestra */
  180. void DisegnaTutto()
  181. {
  182.   register WORD x1,y1,x2,y2,penna,ncol,dx,dy;
  183.  
  184.   ncol = 1 << pubscreen->BitMap.Depth;
  185.   dx = RangeRand(6)+2;
  186.   dy = RangeRand(6)+2;
  187.   penna = RangeRand(ncol-1)+1;
  188.   SetAPen(finestra->RPort,penna); /* imposta il colore da usare per il disegno */
  189.   x2 = 1023; y1 = 0; y2 = 1023;
  190.   for (x1=0; x1<1204; x1+=dx)
  191.   {
  192.     Move(finestra->RPort,x1,y1);
  193.     Draw(finestra->RPort,x2,y2); /* disegna la linea */
  194.     x2-=dx;
  195.   }
  196.   penna = RangeRand(ncol-1)+1;
  197.   SetAPen(finestra->RPort,penna);
  198.   x1 = 0; x2 = 1023; y2 = 1023;
  199.   for (y1=0; y1<1204; y1+=dy)
  200.   {
  201.     Move(finestra->RPort,x1,y1);
  202.     Draw(finestra->RPort,x2,y2);
  203.     y2-=dy;
  204.   }
  205.   return;
  206. }
  207.  
  208. /* questa procedura adatta i dati dei gadgets al cambiamento di dimensione della
  209.    finestra */
  210. void NewSize(px,py)
  211. register LONG *px,*py;
  212. {
  213.   register LONG pp;
  214.   pp = *px;
  215.   if (*px>(1024-finestra->GZZWidth)) *px = 1024-finestra->GZZWidth;
  216.   ScrollLayer(0,finestra->RPort->Layer,*px-pp,0);
  217.   pp = *py;
  218.   if (*py>(1024-finestra->GZZHeight)) *py = 1024-finestra->GZZHeight;
  219.   ScrollLayer(0,finestra->RPort->Layer,0,*py-pp);
  220.  
  221.   NewModifyProp(&GadOriz,finestra,NULL,AUTOKNOB|FREEHORIZ,((*px)*MAXPOT)/(1024-finestra->GZZWidth),0,
  222.                 (finestra->GZZWidth * MAXBODY)/1024,MAXBODY,1);
  223.   NewModifyProp(&GadVert,finestra,NULL,AUTOKNOB|FREEVERT,0,((*py)*MAXPOT)/(1024-finestra->GZZHeight),
  224.                 MAXBODY,(finestra->GZZHeight * MAXBODY)/1024,1);
  225.   return;
  226. }
  227.  
  228. /* muove la superbitmap in base ai valori dei pot dei gadgets proporzionali */
  229. void MoveMap(ID,px,py)
  230. register UWORD ID;
  231. register LONG *px,*py;
  232. {
  233.   register LONG pp;
  234.  
  235.   switch (ID)
  236.   {
  237.     case ORIZGADGET:
  238.       pp = *px;
  239.       *px = (PropOrizInfo.HorizPot*(1024-finestra->GZZWidth))/MAXPOT;
  240.       ScrollLayer(0,finestra->RPort->Layer,*px-pp,0);
  241.       break;
  242.     case VERTGADGET:
  243.       pp = *py;
  244.       *py = (PropVertInfo.VertPot*(1024-finestra->GZZHeight))/MAXPOT;
  245.       ScrollLayer(0,finestra->RPort->Layer,0,*py-pp);
  246.       break;
  247.   }
  248.   return;
  249. }
  250.  
  251. void main()
  252. {
  253.   register LONG pp;
  254.   LONG px=0,py=0;
  255.   struct IntuiMessage mm;
  256.   register UWORD GadID = NESSUNGADGET;
  257.  
  258.   OpenAll();  /* apre tutte le strutture dati */
  259.   NewSize(&px,&py);
  260.   SetDrMd(finestra->RPort,JAM1); /* cancella la BitMap */
  261.   SetAPen(finestra->RPort,0); RectFill(finestra->RPort,0,0,1023,1023);
  262.   DisegnaTutto(); /* disegna le linee */
  263.   /* sposta il layer nella posizione 0,0 */
  264.   pp = finestra->RPort->Layer->Scroll_X; /* determina la posizione X del layer */
  265.   ScrollLayer(0,finestra->RPort->Layer,-pp,0); /* sposta il layer di -X */
  266.   pp = finestra->RPort->Layer->Scroll_Y; /* stesso per Y */
  267.   ScrollLayer(0,finestra->RPort->Layer,0,-pp);
  268.  
  269.   while(1)
  270.   {
  271.     WaitEvent(UPort,&mm);  /* attende un evento */
  272.     if (mm.Class == IDCMP_CLOSEWINDOW) break; /* se gadget di chiusura esci */
  273.     else if (mm.Class == IDCMP_GADGETDOWN)
  274.     {
  275.       GadID = ((struct Gadget *)(mm.IAddress))->GadgetID;
  276.     }
  277.     else if (mm.Class == IDCMP_GADGETUP)
  278.     {
  279.       MoveMap(GadID,&px,&py);
  280.       GadID = NESSUNGADGET;
  281.     }
  282.     else if (mm.Class == IDCMP_NEWSIZE) /* l'utente ha cambiato dimensione, verificare la corretta posizione */
  283.     {
  284.       NewSize(&px,&py);
  285.     }
  286.   }
  287.   CloseAll();  /* chiudi tutto ed esci */
  288. }
  289.